//
//  ViewController.m
//  MusicLrc
//
//  Created by apple on 15/12/26.
//  Copyright © 2015年 CXM. All rights reserved.
//

#import <AVFoundation/AVFoundation.h>
#import "ViewController.h"
#import "JWLrcScrollView.h"
#import "JWAudioTool.h"


@interface ViewController ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet JWLrcScrollView *lrcScrollView;

/** 歌词定时器 */
@property (nonatomic, strong) CADisplayLink *lrcTimer;

/** 播放器 */
@property (nonatomic, weak) AVAudioPlayer *player;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // 1.开始播放歌曲
    AVAudioPlayer *player = [JWAudioTool playMusicWithMusicName:@"10405520.mp3"];
 
    self.player = player;
//    // 2. 设置ScrollView
    self.lrcScrollView.contentSize = CGSizeMake(self.view.bounds.size.width , 0);
    
    // 3. 给lrcScrollView的lrcName设置当前播放歌曲的歌名
    self.lrcScrollView.lrcName = @"10405520.lrc";
   
    // 4. 添加歌词定时器
    [self startLrcTimer];

   
}
- (IBAction)onplay:(id)sender {
    
    [self.player play];
    
}
- (IBAction)onpause:(id)sender {
    [self.player pause];
}
#pragma mark - 歌词定时器的操作
- (void)startLrcTimer {
    
    
    self.lrcTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateLrcInfo)];
    [self.lrcTimer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}

- (void)stopLrcTimer {
    
    [self.lrcTimer invalidate];
    self.lrcTimer = nil;
}

- (void)updateLrcInfo {
    
    
    self.lrcScrollView.currentTime =  self.player.currentTime;
}



#pragma mark - <UIScrollViewDelegate>lrcView代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
